
from pysideuic import compileUi
import os
import argparse

parser = argparse.ArgumentParser(description="ui to py")
parser.add_argument("-i", type=str, default=None)
parser.add_argument("-o", type=str, default=None)
input_f = parser.parse_args().i
output_f = parser.parse_args().o

def validate(file_path):
    if file_path:
        file_path = str(file_path).replace('\\', '/')
        if not os.path.isfile(file_path):
            current_dir = os.getcwd()
            _temp = file_path.split('/')
            if _temp[0] == '.':
                return os.path.join(current_dir, *_temp[1:]).replace('\\', '/')
            elif _temp[0] == '..':
                status = None
                _path = current_dir
                index = 0
                for i, v in enumerate(_temp):
                    if v == '..':
                        _path = os.path.dirname(_path)
                    else:
                        index = i
                        break
                return os.path.join(_path, *_temp[index:]).replace('\\', '/')
            else:
                return os.path.join(current_dir, file_path).replace('\\', '/')
            
        else:
            return os.path.abspath(file_path).replace('\\', '/')
    
input_f = validate(input_f)
output_f = validate(output_f)
if input_f and output_f:
    if not os.path.isfile(input_f):
        raise IOError("File is not exists : %s" % input_f)
    output_dir = os.path.dirname(output_f)
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)
    with open(output_f, 'w') as f:
        compileUi(input_f, f, False, 4, False)

